package com.gxg.web.business;

import com.gxg.common.R;
import com.gxg.config.code.RCode;
import com.gxg.pojo.XUserEntity;
import com.gxg.web.dto.XUserDto;
import com.gxg.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * Created by guxiaoguang on 2018/5/2.
 */
@Component
public class LoginOpearater {

    @Autowired
    private LoginService loginService;

    public R userLogin(XUserDto dto) {
        R r = new R();
        String xUserName = dto.getUsername();
        List<XUserEntity> xUserEntities = loginService.selectUserByName(xUserName);
        if (null == xUserEntities || xUserEntities.size() <= 0) {
            r.put(RCode.CODE, RCode.USER_NOT_EXSIT);
            r.put(RCode.MESSAGE, RCode.USER_NOT_EXSIT_MSG);
            return r;
        }
        XUserEntity xUserEntity = xUserEntities.get(0);
        // 2.判断对象的密码与前端传的密码是否一样
        if (!xUserEntity.getPassword().equals(dto.getPassword())) {
            r.put(RCode.CODE, RCode.ERROR_PASSWORD_CODE);
            r.put(RCode.MESSAGE, RCode.ERROR_PASSWORD_MSG);
            r.put("xuser", "");
            return r;
        }
        r.put(RCode.CODE, RCode.SUCCESS);
        r.put(RCode.MESSAGE, RCode.SUCCESS_MSG);
        r.put("xuser", xUserEntity);
        return r;
    }


}
